<template>
  <div v-if="!loading">
    <bx-table v-if="viewModel==='mlist'&&formCfg&&formCfg.service_name" :tableName="sectionName" :serviceName="formCfg.service_name"
      use_type="updatechildlist" :table-type="'list'" :condition="condition">
      <template v-slot:title>
        <view class="table-name bg-white padding-tb-sm  text-blue">
          <text>{{sectionName||''}}</text>
        </view>
      </template>
    </bx-table>
  </div>
</template>

<script>
  export default {
    props: {
      formCfg: {
        type: Object,
      },
      defaultVal: {
        type: Object
      }
    },
    computed: {
      appName() {
        return uni.getStorageSync('activeApp')
      },
      viewModel() {
        return this.formCfg?.foreign_key?.view_model
      },
      sectionName() {
        return this.formCfg?.foreign_key?.section_name || ''
      }
    },
    data() {
      return {
        colsV2Data: null,
        condition: [],
        loading: true
      }
    },
    created() {
      this.initData()
    },
    methods: {
      initData() {
        if (this.formCfg?.foreign_key?.view_model === 'mlist' && this.formCfg?.service_name) {
          // this.getColV2(this.formCfg.service_name, 'list')
          this.buildCondition()
          this.loading = false
        }
      },
      buildCondition() {
        let foreign_key = this.formCfg?.foreign_key;
        if (foreign_key?.column_name && foreign_key?.referenced_column_name && this.defaultVal[foreign_key
            ?.referenced_column_name]) {
          this.condition = [{
            colName: foreign_key?.column_name,
            ruleType: 'eq',
            value: this.defaultVal[foreign_key?.referenced_column_name]
          }]
        }
      },
      async getColV2(serviceName, type) {
        let colVs = await this.getServiceV2(serviceName, type, type, this.appName);
        this.colsV2Data = colVs;
        console.log('colsV2Data', colVs);
        let fields = [];
        switch (type) {
          case 'update':
            fields = colVs._fieldInfo;
            break;
          case 'add':
            fields = colVs._fieldInfo;
            break;
          case 'detail':
          case 'procdetail':
            fields = this.setFieldsDefaultVal(colVs._fieldInfo, this.defaultVal);
            break;
          default:
            break;
        }
        if (type == 'update') {
          fields = this.setFieldsDefaultVal(colVs._fieldInfo, this.defaultVal);
        }
        if (fields && Array.isArray(fields)) {
          fields = fields.filter((item, index) => {
            if (!item.value) {
              item.value = '';
            }
            return item;
          });
        }
        return fields;
      }
    },
  }
</script>

<style scoped>

</style>
